﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForms_EncodingConverter
{


    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void convert_button_Click(object sender, EventArgs e)
        {
            dst_textbox.Text =
                Encoding.GetEncoding(((EncodingInfoView)dst_combobox.SelectedItem).CodePage).GetString(Encoding.GetEncoding(((EncodingInfoView)src_combobox.SelectedItem).CodePage).GetBytes(src_textbox.Text));
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            List<EncodingInfoView> encodingInfoViewList1 = 
                Encoding.GetEncodings().Select(r => new EncodingInfoView
            {
                CodePage = r.CodePage,
                Name = r.Name,
                DisplayName = string.Format("{0} ({1})", r.DisplayName, r.CodePage)
            }).OrderBy(r => r.DisplayName).ToList();

            List<EncodingInfoView> encodingInfoViewList2 = encodingInfoViewList1.ToList();

            src_combobox.DataSource = encodingInfoViewList1;
            src_combobox.DisplayMember = "DisplayName";

            dst_combobox.DataSource = encodingInfoViewList2;
            dst_combobox.DisplayMember = "DisplayName";

            EncodingInfoView s1 = encodingInfoViewList1.First(r => r.CodePage == 1252);
            EncodingInfoView s2 = encodingInfoViewList2.First(r => r.CodePage == 949);

            src_combobox.SelectedItem = s1;
            dst_combobox.SelectedItem = s2;
        }
    }

    public class EncodingInfoView
    {
        public int CodePage { get; set; }

        public string DisplayName { get; set; }

        public string Name { get; set; }
    }
}
